/*
* @Author: dengjiayao
* @Date:   2017-08-28 16:57:27
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-12-21 08:59:57
*/
import Pages from '../../component/pages/pages.vue'
import { showMsg, unsubscribeCourse, getSubscribeUserList } from '../../vuex/action'

export default {
  name: 'classes-list',
  components: {
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      filter: {
        phone: ''
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.classesListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getSubscribeUserList,
      xUnsubscribe: unsubscribeCourse
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getList({ start = this.pages.start, limit = this.pages.limit }) {
      let params = { start, limit }
      if (this.filter.phone) {
        params.phone = this.filter.phone
      }
      this.xGetList(params)
        .then(data => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.count / limit)
        })
        .catch(res => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    unsubscribe(streamId) {
      this.xUnsubscribe({
        streamId
      }).catch(res => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
